<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:layui=""
      th:replace="pages/common/main :: layout(~{::title}, ~{::section}, ~{}, ~{::script})">
    <head>
        <title>字典</title>
    </head>
    <body>
        <section>

            <div class="layui-fluid">

                <script type="text/html" id="toolBar">
                    <a class="layui-btn layui-btn-normal layui-btn-sm" lay-event="create" layui:auth="/sys/tenant/save">
                        <i class="layui-icon layui-icon-file-b">新建租户</i>
                    </a>
                    <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="deleteBatch"
                       layui:auth="/sys/tenant/batch-delete">
                        <i class="layui-icon layui-icon-delete">批量删除</i>
                    </a>
                </script>

                <script type="text/html" id="tool">

                    {{# if (d.init === 1) { }}
                    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="destroy"
                       layui:auth="/sys/tenant/destroy">销毁系统</a>
                    {{# } else { }}
                    <a class="layui-btn layui-btn-xs" lay-event="init" layui:auth="/sys/tenant/init">初始化系统</a>
                    {{# } }}

                    <a class="layui-btn layui-btn-xs" lay-event="update" layui:auth="/sys/tenant/save">编辑</a>
                    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete"
                       layui:auth="/sys/tenant/delete">删除</a>
                </script>

                <script type="text/html" id="activeTpl">
                    {{# if (d.active === 1) { }}
                    <input type="checkbox" checked lay-skin="switch" lay-text="启用|禁用" lay-filter="switch-filter"
                           data-code="{{d.code}}"/>
                    {{# } else { }}
                    <input type="checkbox" lay-skin="switch" lay-text="启用|禁用" lay-filter="switch-filter"
                           data-code="{{d.code}}"/>
                    {{# } }}
                </script>

                <div class="layui-card">
                    <div class="layui-card-body">
                        <table id="tenant-table" lay-filter="tenant-table-filter"></table>
                    </div>
                </div>

            </div>

        </section>

        <script th:inline="javascript">

            layui.use(["grid", "form", "ax", "jquery", "admin"], function () {
                var grid = layui.grid,
                    form = layui.form,
                    $ax = layui.ax,
                    admin = layui.admin,
                    $ = layui.jquery;

                var dicttype = {}
                    , col = [
                    {type: 'checkbox'},
                    {field: 'init', hide: true},
                    {width: 200, field: 'code', title: "标识符", edit: true},
                    {field: 'name', align: "center", edit: true, title: '名称'},
                    // {field: 'description', align: "center", title: '描述'},
                    {width: 100, field: 'datasource', align: "center", title: '数据源'},
                    {width: 120, field: 'username', align: "center", title: '初始用户'},
                    {width: 120, field: 'password', align: "center", title: '初始密码'},
                    {width: 110, field: 'active', align: "center", title: '状态', templet: "#activeTpl"},
                    // {width: 180, field: 'create_time', align: "center", sort: true, title: '创建时间'},
                    {align: 'center', toolbar: '#tool', title: '操作', width: 220}
                ];

                dicttype.table = {
                    height: "full-98",
                    cols: [col],
                    // url: "",
                    data: /*[[${data}]]*/[]
                };

                dicttype.eventOptions = {
                    create: {
                        url: "/sys/tenant/form"
                    },
                    deleteBatch: {
                        url: "/sys/tenant/batch-delete",
                        condition: "code",
                        after: function (obj, param, that) {
                            admin.refresh()
                        }
                    },
                    update: {
                        url: "/sys/tenant/form",
                        condition: "code"
                    },
                    delete: {
                        url: "/sys/tenant/delete",
                        condition: "code",
                        after: function (obj, param, that) {
                            admin.refresh()
                        }
                    }
                }


                var options = {
                        tableId: "tenant-table",
                        tableFilter: "tenant-table-filter",
                        table: dicttype.table,
                        editor: {
                            url: Kong.ctxPath + "/sys/tenant/edit"
                        },
                        eventOptions: dicttype.eventOptions
                    },
                    events = {
                        destroy: function (obj, config, that) {
                            layer.confirm('销毁后系统数据将丢失', {icon: 3, title: '确定销毁该系统？'}, function (index) {
                                var code = obj.data.code
                                var ajax = new $ax(Kong.ctxPath + "/sys/tenant/destroy", function (data) {
                                    Kong.success("销毁成功！")
                                    admin.refresh()
                                })
                                ajax.setData({code: code})
                                ajax.send()
                                layer.close(index);
                            });
                        },
                        init: function (obj, config, that) {
                            layer.confirm('初始化系统？', {icon: 3, title: '提示'}, function (index) {
                                var code = obj.data.code
                                var ajax = new $ax(Kong.ctxPath + "/sys/tenant/init", function (data) {
                                    Kong.success("初始化完成！")
                                    admin.refresh()
                                })
                                ajax.setData({code: code})
                                ajax.send()
                                layer.close(index);
                            });
                        }
                    };


                grid.render(options, events);

                form.on("switch(switch-filter)", function (data) {
                    var code = data.elem.getAttribute("data-code")
                    var ajax = new $ax(Kong.ctxPath + "/sys/tenant/edit", function (data) {
                        Kong.success("修改成功！")
                    })
                    ajax.setDataDataValue("active", data.elem.checked ? 1 : 0)
                    ajax.setDataConditionValue("code", code)
                    ajax.sendJson()
                });

            });

        </script>


    </body>
</html>
